package yy.yyds.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yy.yyds.basic.query.util.QueryResult;
import yy.yyds.org.domain.Shop;
import yy.yyds.org.mapper.ShopMapper;
import yy.yyds.org.query.ShopQuery;
import yy.yyds.org.service.IShopService;

import java.util.List;
@Service
/*读事务的注解*/
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ShopServiceimpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Override
    /*写事务*/
    @Transactional
    public void add(Shop shop) {
        shopMapper.add(shop);
    }

    @Override
    /*写事务*/
    @Transactional
    public void delete(Long id) {
        shopMapper.delete(id);
    }

    @Override
    /*写事务*/
    @Transactional
    public void update(Shop shop) {
        shopMapper.update(shop);
    }

    @Override
    public Shop loadOne(Long id) {
        return shopMapper.loadOne(id);
    }

    @Override
    public List<Shop> loadAll() {
        return shopMapper.loadAll();
    }

    @Override
    public QueryResult<Shop> querypage(ShopQuery shopQuery) {
        /*查询总条数*/
        Integer total = shopMapper.queryCount(shopQuery);
        if (total == 0){//如果总条数已经是0条 就不需要后面的查询
//            ctrl + alt + 向右或向左 反复横跳
            return new QueryResult<>();
        }else {
            /*查询当前页数据*/
            List<Shop> departments = shopMapper.queryList(shopQuery);
            return new QueryResult<>(total,departments);
        }
    }
}
